Check if a String is Anagram

Theory:

An anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once.

Python Code:

def is_anagram(str1, str2):
    return sorted(str1) == sorted(str2)

# Taking input for strings and checking if they are anagrams
def check_and_display_anagram():
    str1 = input("Enter the first string: ")
    str2 = input("Enter the second string: ")
    if is_anagram(str1, str2):
        print("The strings are anagrams.")
    else:
        print("The strings are not anagrams.")

check_and_display_anagram()

Example Output 1:

Enter the first string: listen

Enter the second string: silent

The strings are anagrams.

Example Output 2:

Enter the first string: hello

Enter the second string: world

The strings are not anagrams.

Code Explanation:

The function is_anagram(str1, str2) checks whether two strings are anagrams of each other by sorting their characters and comparing them.

The function check_and_display_anagram() takes input for two strings, checks if they are anagrams using the aforementioned function, and prints the result.